package com.example.job.Activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.job.R;

import com.example.job.db.userdb;
import com.example.job.entity.UserInfo;

public class UpdateActivity extends AppCompatActivity {
    private EditText et_password;

    private EditText check_psd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_update);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        et_password = findViewById(R.id.et_password);
        check_psd = findViewById(R.id.check_psd);

        findViewById(R.id.update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String new_pwd = et_password.getText().toString();
                String check_pwd = check_psd.getText().toString();

                if(TextUtils.isEmpty(new_pwd)){
                    Toast.makeText(UpdateActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                } else if (!new_pwd.equals(check_pwd)) {
                    Toast.makeText(UpdateActivity.this,"两次密码不一致",Toast.LENGTH_SHORT).show();
                } else{
                    UserInfo userInfo = UserInfo.getUserInfo(UpdateActivity.this);
                    if(userInfo != null){
                        int row = (int) userdb.getInstance(UpdateActivity.this).update(userInfo.getUser_id(),new_pwd);
                        if(row > 0){
                            Toast.makeText(UpdateActivity.this,"修改成功，请重新登录",Toast.LENGTH_SHORT).show();
                            setResult(1000);
                            finish();
                        }else{
                            Toast.makeText(UpdateActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            }
        });

    }
}